package com.xyd.Servlet;

import com.xyd.Biz.Impl.UserAddBizImpl;
import com.xyd.Biz.UserAddBiz;
import com.xyd.Entity.ManageUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/html/manage/UserAddServlet")
public class UserAddServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("userName");
        String pwd = request.getParameter("passWord");
        String power = request.getParameter("poWer");
        String realname = request.getParameter("name");
        String realnnumber = request.getParameter("num");
        String birthyear = request.getParameter("birthyear");
        String birthmonth = request.getParameter("birthmonth");
        String birthday = request.getParameter("birthday");
        String sex = request.getParameter("sex");
        String phonenum = request.getParameter("mobile");

        String birth = birthyear + "-" + birthmonth + "-" + birthday;

        UserAddBiz userAddBiz = new UserAddBizImpl();
        ManageUser manageUser = new ManageUser(0, name, pwd, Integer.parseInt(power), realname, realnnumber, birth, sex, phonenum);

        int manageUser1 = userAddBiz.UserAdd(manageUser);

        if (manageUser1 > 0) {
            request.getRequestDispatcher("index.jsp").forward(request, response);
//            request.getRequestDispatcher("/index.jsp").forward(request, response);
        } else {
            response.sendRedirect("user-add.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
